﻿/**
 * File:   sonar_view.c
 * Author: 
 * Brief:  
 *
 * Copyright (c) 2024 - 2024 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * License file for more details.
 *
 */

/**
 * History:
 * ================================================================
 * 2024-8-18  created
 *
 */


#include "tkc/mem.h"
#include "tkc/utils.h"
#include "base/widget.h"
#include "widgets/view.h"
#include "sonar_view.h"

static ret_t on_view1_click(void* ctx, event_t* e) {
  pointer_event_t* evt = pointer_event_cast(e);
  // TODO: 在此添加控件事件处理程序代码
  printf("view1 click\r\n");
  return RET_OK;
}

static ret_t on_view2_click(void* ctx, event_t* e) {
  pointer_event_t* evt = pointer_event_cast(e);
  // TODO: 在此添加控件事件处理程序代码
  printf("view2 click\r\n");
  return RET_OK;
}

static ret_t on_view3_click(void* ctx, event_t* e) {
  pointer_event_t* evt = pointer_event_cast(e);
  // TODO: 在此添加控件事件处理程序代码
  printf("view3 click\r\n");
  return RET_OK;
}

static ret_t sonar_view_get_prop(widget_t* widget, const char* name, value_t* v) {
  sonar_view_t* sonar_view = SONAR_VIEW(widget);
  return_value_if_fail(sonar_view != NULL && name != NULL && v != NULL, RET_BAD_PARAMS);



  return RET_NOT_FOUND;
}

static ret_t sonar_view_set_prop(widget_t* widget, const char* name, const value_t* v) {
  sonar_view_t* sonar_view = SONAR_VIEW(widget);
  return_value_if_fail(widget != NULL && name != NULL && v != NULL, RET_BAD_PARAMS);



  return RET_NOT_FOUND;
}

static ret_t sonar_view_on_destroy(widget_t* widget) {
  sonar_view_t* sonar_view = SONAR_VIEW(widget);
  return_value_if_fail(widget != NULL && sonar_view != NULL, RET_BAD_PARAMS);



  return RET_OK;
}


static ret_t draw_triangle(widget_t *widget, canvas_t *c,  point_t p1, point_t p2, point_t p3) {
  /* 获取控件风格*/
  style_t* style = widget->astyle;
  color_t trans = color_init(0, 0, 0, 0);
  vgcanvas_t* vg = canvas_get_vgcanvas(c);
  /* 获取风格中设置的 bg_color 颜色值 */
  color_t color = style_get_color(style, STYLE_ID_BG_COLOR, trans);

  /* 保存之前 vgcanvas 的状态 */
  vgcanvas_save(vg);

  /* 使用 vgcanvas 来画出三角形的样子 */
  vgcanvas_begin_path(vg);
  /* 因为 vgcanvas 画图的坐标是绝对坐标，相对整个程序来说的 */
  /* 而 c->ox 等于控件的获取绝对坐标，y坐标同理 */
  vgcanvas_move_to(vg, c->ox + widget->x + p1.x, c->oy + widget->y + p1.y);
  vgcanvas_line_to(vg, c->ox + widget->x + p2.x, c->oy + widget->y + p2.y);
  vgcanvas_line_to(vg, c->ox + widget->x + p3.x, c->oy + widget->y + p3.y);
  vgcanvas_close_path(vg);

  /* 设置填充颜色 */
  vgcanvas_set_fill_color(vg, color);
  /* 开始填充 */
  vgcanvas_fill(vg);

  /* 还原到之前 vgcanvas 的状态 */
  vgcanvas_restore(vg);
  
  return RET_OK;
}

static ret_t sonar_view_paint_children(widget_t *child, canvas_t* c){
   /* need update style of children widget otherwise the state will remain unstage when event triggered */
  if(child->need_update_style){
    widget_update_style(child);
  }

   /* draw children widget canvas path */
  draw_triangle(child, c, (point_t){0, 0}, (point_t){0, child->h}, (point_t){child->w, child->h / 2});

  /* draw children widget text and border */
  rect_t r = rect_init(child->x, child->y, child->w, child->h);
  canvas_translate(c, child->x, child->y);
  widget_on_paint_border(child, c);
  widget_paint_helper(child, c, NULL, NULL);
  canvas_untranslate(c, child->x, child->y);
  return RET_OK;
}

static ret_t sonar_view_on_paint_children(widget_t* widget, canvas_t* c) {
  widget_t *view1 = widget_lookup(widget, SONAR_VIEW_SUB_WIDGET_VIEW1, TRUE);
  widget_t *view2 = widget_lookup(widget, SONAR_VIEW_SUB_WIDGET_VIEW2, TRUE);
  widget_t *view3 = widget_lookup(widget, SONAR_VIEW_SUB_WIDGET_VIEW3, TRUE);
  return_value_if_fail(view1 != NULL && view2 != NULL && view3 != NULL, RET_BAD_PARAMS);

  sonar_view_paint_children(view1, c);
  sonar_view_paint_children(view2, c);
  sonar_view_paint_children(view3, c);
  return RET_OK;
}

static ret_t sonar_view_on_paint_self(widget_t* widget, canvas_t* c) {
  sonar_view_t* sonar_view = SONAR_VIEW(widget);

  (void)sonar_view;

  return RET_OK;
}



static ret_t sonar_view_on_event(widget_t* widget, event_t* e) {
  sonar_view_t* sonar_view = SONAR_VIEW(widget);
  return_value_if_fail(widget != NULL && sonar_view != NULL, RET_BAD_PARAMS);
  widget_t *view1 = widget_lookup(widget, SONAR_VIEW_SUB_WIDGET_VIEW1, TRUE);
  widget_t *view2 = widget_lookup(widget, SONAR_VIEW_SUB_WIDGET_VIEW2, TRUE);
  widget_t *view3 = widget_lookup(widget, SONAR_VIEW_SUB_WIDGET_VIEW3, TRUE);
  if(!(view1 != NULL && view2 != NULL && view3 != NULL)){
    return RET_BAD_PARAMS;
  }
  if(!sonar_view->inited){
    widget_on(view1, EVT_CLICK, on_view1_click, widget);
    widget_on(view2, EVT_CLICK, on_view2_click, widget);
    widget_on(view3, EVT_CLICK, on_view3_click, widget);
    sonar_view->inited = TRUE;
  }

  (void)sonar_view;

  return RET_OK;
}

const char* s_sonar_view_properties[] = {
  NULL
};

TK_DECL_VTABLE(sonar_view) = {.size = sizeof(sonar_view_t),
                          .type = WIDGET_TYPE_SONAR_VIEW,
                          .clone_properties = s_sonar_view_properties,
                          .persistent_properties = s_sonar_view_properties,
                          .parent = TK_PARENT_VTABLE(widget),
                          .create = sonar_view_create,
                          .on_paint_self = sonar_view_on_paint_self,
                          .on_paint_children = sonar_view_on_paint_children,
                          .set_prop = sonar_view_set_prop,
                          .get_prop = sonar_view_get_prop,
                          .on_event = sonar_view_on_event,
                          .on_destroy = sonar_view_on_destroy};

widget_t* sonar_view_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
  widget_t* widget = widget_create(parent, TK_REF_VTABLE(sonar_view), x, y, w, h);
  sonar_view_t* sonar_view = SONAR_VIEW(widget);
  return_value_if_fail(sonar_view != NULL, NULL);


  return widget;
}

widget_t* sonar_view_cast(widget_t* widget) {
  return_value_if_fail(WIDGET_IS_INSTANCE_OF(widget, sonar_view), NULL);

  return widget;
}
